#!/usr/bin/perl 

use strict;
use Data::Dumper;
use lib('../bin');
use lib::TextTreatment;
use Devel::Size qw(size total_size);
use Benchmark::Stopwatch;

=head1 nplg_text.pl

    perl nplg_text.pl
    Ce script donne la quantite de memoire utilisee et le temps d'execution a different point.

=cut

my $stopwatch = Benchmark::Stopwatch->new->start;

# --------------------------------
# -1- On prend le texte en entree
# --------------------------------

open (FD, "LesMysteresDeParis.txt") or die $!;
read(FD, my $text, -s FD);

$stopwatch->lap('read LesMysteresDeParis');
my $text_size = size($text);
print "Taille du texte = $text_size\n";

# --------------------------------
# -2- heuristique de decoupage
# --------------------------------
use lib::SplitText;
my @text_to_search; # contient la concatenation de toutes les heuristiques

# decoupage tous les 15 mots
my $text_decoupe_mot = &decoupe_par_mot($text,15, 20);

$stopwatch->lap('Decoupage par mot');

foreach(@$text_decoupe_mot){
    push @text_to_search, $_;
}

# --------------------------------
# -3- Envoie des requetes
# --------------------------------
use lib::Req;
my $text_langage = &get_language;
my @results;
my $req_count = 0;
foreach(@text_to_search){
    my $inner_result = request(
        {
            text   => $_,
            engine => "Yahoo",
            count  => "1",
            lang   => $text_langage,
        }
    );
    $req_count++;
    foreach (@$inner_result){
        push @results, $_;
    }
}
print "Nombre de requetes = ".$req_count."\n";
print "Taille du tableau de resultat = ".total_size(\@results)."\n";
$stopwatch->lap('Requete effectue');

# --------------------------------
# -4- Traitement des resultats
# --------------------------------
use lib::ResultTreatment;
my $results_treated = &result_treatment(\@results);
$stopwatch->lap('Triage des resultats');

# --------------------------------
# -5- Fouille des sites interressants
# --------------------------------
use lib::Wget;
$results_treated = wget($results_treated);
print "Taille du tableau de resultat+site distant =".total_size($results_treated)."\n";
$stopwatch->lap('Fouille des sites distants');
print $stopwatch->stop->summary;
print "\nStructure finale =>\n".Dumper($results_treated);
